package com.skymr.sms.ui;

import com.skymr.baidumap.service.InvokeLater;
import com.skymr.baidumap.service.LaterThread;
import com.skymr.baidumap.service.SMSSender;
import com.skymr.sms.model.Contactor;
import com.skymr.sms.util.SwingUtil;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SMSFrame extends javax.swing.JFrame {

    /** Creates new form SMSFrame */
    public SMSFrame() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("免费短信");

        jLabel1.setText("发送人：");

        jLabel2.setText("接收者：");

        jLabel3.setText("内容：");

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("发送");

        jButton2.setText("选择");

        jButton3.setText("选择");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(394, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(156, 156, 156))
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jTextField2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 357, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 357, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 97, Short.MAX_VALUE))
                        .addGap(30, 30, 30))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jButton2)))
                .addGap(51, 51, 51)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(52, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public boolean validatePhoneNumber(){
        String fromPhone = this.jTextField1.getText();
        String toPhone = this.jTextField2.getText();
        fromPhone = fromPhone.replaceAll("\\(.{1,}\\)", "");
        toPhone = toPhone.replaceAll("\\(.{1,}\\)", "");
        java.util.regex.Pattern p = Pattern.compile("1[0-9]{10}");
        Matcher m = p.matcher(fromPhone);
        if(!m.matches()){
            javax.swing.JOptionPane.showMessageDialog(this, "发送人号码格式不正确");
            return false;
        }
        m = p.matcher(toPhone);
        if(!m.matches()){
            javax.swing.JOptionPane.showMessageDialog(this, "接收者号码格式不正确");
            return false;
        }
        return true;
    }
    
    public void sendSMS(){
        try{
            if(!validatePhoneNumber()){
                return ;
            }
            SMSSender ss = new SMSSender();
            String fromPhone = jTextField1.getText().replaceAll("\\(.{1,}\\)", "");
            String toPhone = jTextField2.getText().replaceAll("\\(.{1,}\\)", "");
            ss.setFromPhone(fromPhone);
            ss.setToPhone(toPhone);
            if(!ss.isActivate()){
                if(ss.getActiveCode()){
                        String code = javax.swing.JOptionPane.showInputDialog(this, "请输入激活码:");
                        if(ss.isActivate(code)){
                            javax.swing.JOptionPane.showMessageDialog(this, "激活成功");
                            ss.sendSMSMsg(this.jTextArea1.getText());
                        }
                        else{
                            javax.swing.JOptionPane.showMessageDialog(this, "激活失败");
                            return;
                        }
                }
                else{
                    javax.swing.JOptionPane.showMessageDialog(this, "获取激活码失败");
                    return;
                }
            }
            else{
                this.jLabel4.setText("");
                InvokeLater.invokeLater(new LaterThread(this.jLabel4,ss,this.jTextArea1.getText()));
            }
        }catch(Exception e){
            javax.swing.JOptionPane.showMessageDialog(this, "发送短信出错："+ e.getMessage());
        }
    }
    public void init(){
        ActionListener l = new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if("发送".equals(e.getActionCommand())){
                    sendSMS();
                }
                else if("选择".equals(e.getActionCommand())){
                    Contactor c = chooseContactor();
                    if(e.getSource() == jButton2){
                        jTextField2.setText(c.toTextString());
                    }
                    else if(e.getSource() == jButton3){
                        jTextField1.setText(c.toTextString());
                    }
                }
            }
        };
        this.jButton1.addActionListener(l);
        this.jButton2.addActionListener(l);
        this.jButton3.addActionListener(l);
    }
    
    public Contactor chooseContactor(){
        ContactDialog d = new ContactDialog(this,true);
        SwingUtil.setCenter(d);
        d.setVisible(true);
        return d.getResult();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}
